﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-2-22</createdate>
//<author>zhengw</author>
//<email>zhengw@tunynet.com</email>
//<log date="2008-2-22">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Serialization;
using ChinaCustoms.Qingdao.Workflow.Permissions;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 任务实体类
    /// </summary>
    [Serializable]
    public class TaskInfo : ExtendedAttributes
    {
        private Guid taskGuid;
        /// <summary>
        /// 任务Guid
        /// </summary>
        public Guid TaskGuid
        {
            get { return this.taskGuid; }
            set { this.taskGuid = value; }
        }

        private Guid workflowGuid = Guid.Empty;
        /// <summary>
        /// 工作流GUID
        /// </summary>
        public Guid WorkflowGuid
        {
            get { return workflowGuid; }
            set { this.workflowGuid = value; }
        }

        private string taskName = string.Empty;
        /// <summary>
        /// 任务名称
        /// </summary>
        public string TaskName
        {
            get { return this.taskName; }
            set { this.taskName = value; }
        }

        private string taskType= string.Empty;
        /// <summary>
        /// 任务的类型
        /// </summary>
        public string TaskType
        {
            get { return taskType; }
            set { taskType = value; }
        }


        private string description = string.Empty;
        /// <summary>
        /// 任务描述
        /// </summary>
        public string Description
        {
            get { return this.description; }
            set { this.description = value; }
        }

        private DateTime createDate;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDate
        {
            get { return this.createDate; }
            set { this.createDate = value; }
        }

        private DateTime lastUpdateDate;
        /// <summary>
        /// 上次更新日期
        /// </summary>
        public DateTime LastUpdateDate
        {
            get { return this.lastUpdateDate; }
            set { this.lastUpdateDate = value; }
        }

        private string applicationCodeName = string.Empty;
        /// <summary>
        /// 任务所属应用程序CodeName
        /// </summary>
        public string ApplicationCodeName
        {
            get { return applicationCodeName; }
            set { applicationCodeName = value; }
        }

        private string sponsorUserName = string.Empty;
        /// <summary>
        /// 任务发起人UserName
        /// </summary>
        public string SponsorUserName
        {
            get { return sponsorUserName; }
            set { sponsorUserName = value; }
        }

        private TaskStatus status = TaskStatus.Creating;
        /// <summary>
        /// 任务状态
        /// </summary>
        public TaskStatus Status
        {
            get { return this.status; }
            set { this.status = value; }
        }

        private string executingActivityCodeName = string.Empty;
        /// <summary>
        /// 正在执行的工作流活动编码
        /// </summary>
        public string ExecutingActivityCodeName
        {
            get { return this.executingActivityCodeName; }
            set { this.executingActivityCodeName = value; }
        }

        #region 序列化属性

        /// <summary>
        /// 应用程序名称
        /// </summary>
        public string ApplicationName
        {
            get { return GetString("ApplicationName", string.Empty); }
            set { SetExtendedAttribute("ApplicationName", value); }
        }

        /// <summary>
        /// 任务发起人姓名
        /// </summary>
        public string SponsorDisplayName
        {
            get { return GetString("SponsorDisplayName", string.Empty); }
            set { SetExtendedAttribute("SponsorDisplayName", value); }
        }

        /// <summary>
        /// 任务发起人FullPath
        /// </summary>
        public string SponsorFullPath
        {
            get { return GetString("SponsorFullPath", string.Empty); }
            set { SetExtendedAttribute("SponsorFullPath", PermissionUtils.FormatUserFullPath(value)); }
        }

        /// <summary>
        /// 最后一个工作项，即办结任务的工作项
        /// </summary>
        /// <remarks>在办结任务的时候记录当前的工作项，作为任务重新启动时的恢复点</remarks>
        public Guid LastWorkItemGuid
        {
            get { return GetGuid("LastWorkItemGuid", Guid.Empty); }
            set { SetExtendedAttribute("LastWorkItemGuid", value.ToString()); }
        }

        #endregion


        #region 扩展属性

        private ActivityInstanceInfo executingActivityInMainFlow = null;
        /// <summary>
        /// 正在执行的活动原型
        /// </summary>
        public ActivityInstanceInfo ExecutingActivityInMainFlow
        {
            get
            {
                if (executingActivityInMainFlow == null)
                    executingActivityInMainFlow = ActivityInstances.GetLastActivityByCodeName(this.WorkflowGuid, Guid.Empty, this.ExecutingActivityCodeName);

                return executingActivityInMainFlow;
            }
        }

        private string executingStepName = null;
        /// <summary>
        /// 正在执行的步骤名称
        /// </summary>
        /// <remarks>
        /// 主流程中即为活动名称，子流程中根据是否启动子流程显示不同名称
        /// </remarks>
        public string ExecutingStepName
        {
            get
            {
                if (executingStepName == null)
                {
                    if (this.Status == TaskStatus.Completed)
                        executingStepName = "已办结";
                    else if (this.Status == TaskStatus.Terminated)
                        executingStepName = "已终止";
                    else
                    {
                        //if (!ExecutingActivity.IsSubFlow)
                        //{
                        //    executingStepName = ExecutingActivity.ActivityName;
                        //}
                        if (ExecutingActivityInMainFlow != null)
                        {
                            executingStepName = ExecutingActivityInMainFlow.ActivityName;
                        }
                    }
                }

                return executingStepName;
            }

        }

        /// <summary>
        /// 状态对外显示的文字
        /// </summary>
        public string StatusText
        {
            get
            {
                switch (this.Status)
                {
                    case TaskStatus.Creating:
                        return "未生效";
                    case TaskStatus.Executing:
                        return "运行中";
                    case TaskStatus.Completed:
                        return "已办结";
                    case TaskStatus.Terminated:
                        return "已终止";
                    default:
                        return string.Empty;
                }
            }
        }


        #endregion

    }
}
